package com.lifeservice.console.controller;

import com.lifeservice.common.annotation.Log;
import com.lifeservice.common.core.controller.BaseController;
import com.lifeservice.common.core.domain.AjaxResult;
import com.lifeservice.common.core.page.TableDataInfo;
import com.lifeservice.common.enums.BusinessType;
import com.lifeservice.common.utils.SnowFlakeUtil;
import com.lifeservice.common.utils.poi.ExcelUtil;
import com.lifeservice.console.domain.LifeService;
import com.lifeservice.console.service.ILifeServiceService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.Date;
import java.util.List;

/**
 * 便捷服务信息Controller
 *
 * @author lifeservice
 * @date 2023-03-01
 */
@RestController
@RequestMapping("/console/service")
public class LifeServiceController extends BaseController {
    @Autowired
    private ILifeServiceService lifeServiceService;

    /**
     * 查询便捷服务信息列表
     */
    @GetMapping("/list")
    public TableDataInfo list(LifeService lifeService) {
        startPage();
        List<LifeService> list = lifeServiceService.selectLifeServiceList(lifeService);
        return getDataTable(list);
    }

    @PutMapping("/isHide")
    public AjaxResult updateIsHide(@RequestBody LifeService lifeService) {
        return lifeServiceService.updateIsHide(lifeService) > 0 ? AjaxResult.success() : AjaxResult.error("操作失败");
    }

    /**
     * 导出便捷服务信息列表
     */
    @Log(title = "便捷服务信息", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, LifeService lifeService) {
        List<LifeService> list = lifeServiceService.selectLifeServiceList(lifeService);
        ExcelUtil<LifeService> util = new ExcelUtil<LifeService>(LifeService.class);
        util.exportExcel(response, list, "便捷服务信息数据");
    }

    /**
     * 获取便捷服务信息详细信息
     */
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") String id) {
        return success(lifeServiceService.selectLifeServiceById(id));
    }

    /**
     * 新增便捷服务信息
     */
    @Log(title = "便捷服务信息", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody LifeService lifeService) {
        lifeService.setId(SnowFlakeUtil.getInstance().nextId());
        lifeService.setCreateTime(new Date());
        lifeService.setUpdateTime(new Date());
        return toAjax(lifeServiceService.insertLifeService(lifeService));
    }

    /**
     * 修改便捷服务信息
     */
    @Log(title = "便捷服务信息", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody LifeService lifeService) {
        return toAjax(lifeServiceService.updateLifeService(lifeService));
    }

    /**
     * 删除便捷服务信息
     */
    @Log(title = "便捷服务信息", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable String[] ids) {
        return toAjax(lifeServiceService.deleteLifeServiceByIds(ids));
    }
}
